package com.ddtech.business.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ddtech.business.common.BaseEntity;
import com.ddtech.business.common.BaseService;
import com.ddtech.business.constant.BusinessEnum;
import com.ddtech.business.course.entity.CourseResource;
import com.ddtech.business.course.service.CourseResourceService;
import com.ddtech.business.train.entity.TrainPlanResource;
import com.ddtech.business.train.service.TrainPlanResourceService;
import com.ddtech.business.web.entity.Note;
import com.ddtech.business.web.mapper.NoteMapper;
import com.ddtech.business.web.query.NoteQuery;
import com.ddtech.business.web.service.NoteService;
import com.ddtech.exception.BizPreconditions;
import com.ddtech.utils.PageUtil;
import com.ddtech.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 笔记表 服务实现类
 * </p>
 *
 * @author bin_pu
 * @since 2023-02-06
 */
@Service
public class NoteServiceImpl extends BaseService<NoteMapper, Note> implements NoteService {

    @Autowired
    private CourseResourceService courseResourceService;

    @Autowired
    private TrainPlanResourceService trainPlanResourceService;

    @Override
    public Object pageList(NoteQuery noteQuery) {
        IPage<Note> page = this.page(noteQuery.populatePage(), noteQuery.autoWrapper());
        return PageUtil.toPage(page.getRecords(), page.getTotal());
    }

    @Override
    public Object getPageListPc(NoteQuery noteQuery) {
        if(noteQuery.isSelf()){
            //只看自己笔记
            noteQuery.setUserId(SecurityUtils.getUserId());
        }
        IPage<Note> page = this.page(noteQuery.populatePage(), noteQuery.autoWrapper());
        return PageUtil.toPage(page.getRecords(), page.getTotal());
    }

    /***
     * description: 普通课程笔记记录
     * @param:  * @param note
     * @date: 2023-06-27
     * @return: void
     * @version v1.0
     * @author:wei_zhang(xiao)
     */
    @Override
    public void create(Note note) {
        CourseResource courseResource= courseResourceService.getById(note.getChapterId());
        if (courseResource!=null) {
            note.setChapterName(courseResource.getResourceName());
        }
        note.init();
        note.setUserId(SecurityUtils.getUserId());
        note.setUserName(SecurityUtils.getName());
        this.saveOrUpdate(note);
    }
    /***
     * description: 培训课程记录笔记
     * @param:  * @param note
     * @date: 2023-06-27
     * @return: void
     * @version v1.0
     * @author:wei_zhang(xiao)
     */
    @Override
    public void trainCreate(Note note) {
        TrainPlanResource trainPlanResource= trainPlanResourceService.getById(note.getChapterId());
        if(trainPlanResource!=null){
            note.setChapterName(trainPlanResource.getResourceName());
            note.setChapterId(trainPlanResource.getId());
        }
        note.init();
        note.setUserId(SecurityUtils.getUserId());
        note.setUserName(SecurityUtils.getName());
        this.saveOrUpdate(note);
    }

    @Override
    public void del(String id) {
        QueryWrapper<Note> noteQueryWrapper = this.newValidQueryWrapper();
        noteQueryWrapper.eq(BaseEntity.ID, id);
        List<String> roleCode = SecurityUtils.getRoleCode();
        if (!roleCode.contains("ADMIN")) {
            noteQueryWrapper.eq(Note.USER_ID, SecurityUtils.getUserId());
        }
        Note note = this.getOne(noteQueryWrapper);
        BizPreconditions.check(note == null, BusinessEnum.QUERY_ERROR.getMsg());
        note.baseDelete();
        this.saveOrUpdate(note);
    }
}
